#include<iostream>

using namespace std;

// 求一个数的n次方

bool equal(double num1, double num2)
{
    if ((num1 - num2 > -0.0000001) && (num1 - num2 < 0.0000001))
        return true;
    else
        return false;
}

double power(double base, int component){
    // 处理非法数据标志：base = 0, component 为负数
    bool g_invaildInput = false;

    // float、double进行数值比较时要注意精度问题，　一般使用equal()
    if ( equal(base, 0.0) && component < 0 ){
        g_invaildInput = true;
        return 0.0;
    }
    unsigned int absComponet = (unsigned int)component;
    if ( component < 0 ){
        absComponet = (unsigned int)(-component);
    }

    double result = 1;
    for ( int i = 1; i<absComponet; i++ ){
        result *= base;
    }

    if ( component < 0 ){
        result = 1.0/result;
    }
    return result ;
}

int main(void){

    cout <<  "power: " << power(2, 3) << endl;

    // 底数为负数、指数为正数
    cout <<  "power: " << power( -2, 3)<< endl;

    // 指数为负数
    cout <<  "power: " << power(2, -3)<< endl;

    // 指数为0
    cout <<  "power: " << power(2, 0)<< endl;

    // 底数、指数都为0
    cout <<  "power: " << power(0, 0)<< endl;

    // 底数为0、指数为正数
    cout <<  "power: " << power(0, 4)<< endl;

    // 底数为0、指数为负数
    cout <<  "power: " <<  power(0, -4)<< endl;
    return 0;
}